<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ admin.name }}</span>
        <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>
      </div>
      <div class="text item">
        <div class="avatar_warp">
          <el-upload
              :action="'/admin/userface?id=' + admin.id"
              :headers="headers"
              :show-file-list="false"
              :on-success="onSuccess"
              accept=".jpg,.jpeg,.png"
             >
            <img title="点击修改头像" :src="admin.userFace" style="height: 100px;width: 100px;border-radius: 50px ">
          </el-upload>
        </div>
        <div class="list_warp">
          电话号码：
          <el-tag>{{ admin.telephone }}</el-tag>
        </div>
        <div class="list_warp">
          手机号码：
          <el-tag>{{ admin.phone }}</el-tag>
        </div>
        <div class="list_warp">
          居住地址：
          <el-tag>{{ admin.address }}</el-tag>
        </div>
        <div class="list_warp">
          用户标签：
          <el-tag
              class="role_item"
              type="success"
              v-for="(role, index) in admin.roles"
              :key="index"
          >{{ role.nameZh }}
          </el-tag
          >
        </div>
        <div class="list_warp edit">
          <el-button type="primary" @click="showUpdateAdminView"
          >修改信息
          </el-button
          >
          <el-button type="danger" @click="showUpdatePassView"
          >修改密码
          </el-button
          >
        </div>
      </div>
    </el-card>

    <!--修改-->
    <!-- 修改用户信息 -->
    <el-dialog title="修改用户信息" :visible.sync="dialogVisible" width="30%">
      <div>
        <table>
          <tr>
            <td>用户昵称：</td>
            <td>
              <el-input
                  v-model="admin2.name"
                  placeholder="请输入用户昵称"
              ></el-input>
            </td>
          </tr>
          <tr>
            <td>电话号码：</td>
            <td>
              <el-input
                  v-model="admin2.telephone"
                  placeholder="请输入电话号码"
              ></el-input>
            </td>
          </tr>
          <tr>
            <td>手机号码：</td>
            <td>
              <el-input
                  v-model="admin2.phone"
                  placeholder="请输入手机号码"
              ></el-input>
            </td>
          </tr>
          <tr>
            <td>用户住址：</td>
            <td>
              <el-input
                  v-model="admin2.address"
                  placeholder="请输入用户住址"
              ></el-input>
            </td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateAdminInfo">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改密码 -->
    <el-dialog
        title="修改用户密码"
        :visible.sync="passwordDialogVisible"
        width="30%"
    >
      <div>
        <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="dialog_wap"
        >
          <el-form-item label="旧密码" prop="oldPass">
            <el-input
                type="password"
                v-model="ruleForm.oldPass"
                autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="pass">
            <el-input
                type="password"
                v-model="ruleForm.pass"
                autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input
                type="password"
                v-model="ruleForm.checkPass"
                autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
            >提交
            </el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "AdminInfo",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      // 展示数据
      admin: [],
      // 修改数据
      admin2: {
        name: '',
        telephone: '',
        phone: '',
        address: '',
      },
      // 编辑弹出框
      dialogVisible: false,
      // 密码弹出框
      passwordDialogVisible: false,
      ruleForm: {
        oldPass: "",
        pass: "",
        checkPass: "",
      },
      rules: {
        pass: [{validator: validatePass, trigger: "blur"}],
        checkPass: [{validator: validatePass2, trigger: "blur"}],
        oldPass: [{validator: validatePass, trigger: "blur"}],
      },
      headers : {
        Authorization: window.sessionStorage.getItem("tokenStr"),
      }

    };
  },
  methods: {
    // 上传成功!
    onSuccess() {
      this.initAdmin();
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 密码校验
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.adminId = this.admin.id;
          this.putRequest('/admin/pass', this.ruleForm).then((resp) => {
            if (resp) {
              // 成功 退出登录
              // 删除成功 清空用户信息
              this.postRequest('/logout')
              window.sessionStorage.removeItem("tokenStr");
              window.sessionStorage.removeItem("user");
              // 清除菜单信息
              this.$store.commit("initRoutes", []);
              // 跳转到登录页
              this.$router.replace("/");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 修改用户信息
    updateAdminInfo() {
      this.putRequest('/admin/info', this.admin2).then(resp => {
        if (resp) {
          this.dialogVisible = false;
          this.initAdmin();
        }
      })
    },
    // 修改密码弹出框
    showUpdatePassView() {
      this.passwordDialogVisible = true;
    },
    // 修改用户信息弹出框
    showUpdateAdminView() {
      this.dialogVisible = true;
    },
    initAdmin() {
      this.getRequest('/admin/info').then(resp => {
        if (resp) {
          this.admin = resp;
          // this.id = this.admin.id;
          // 对象拷贝
          this.admin2 = Object.assign({}, this.admin);
          window.sessionStorage.setItem("user", JSON.stringify(resp));
          this.$store.commit("INIT_ADMIN", resp);
        }
      })
    }
  },
  mounted() {
    this.initAdmin();
  }
}
</script>

<style scoped>
.box-card {
  width: 600px;
}

.avatar_warp {
  display: flex;
  justify-content: center;
}

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50px;
}

.list_warp {
  margin: 8px 0;
}

.edit {
  display: flex;
  justify-content: space-around;
}

.role_item {
  margin-right: 6px;
}

.dialog_wap {
  padding-right: 30px;
}
</style>
